package com.study.talk.ui.app.group

import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar
import com.study.talk.ui.adapter.UsersSelectAdapter
import com.study.talk.ui.app.base.BaseActivity
import com.study.talk.netty.NettyClient
import com.study.talk.netty.message.request.InviteGroupRequest
import com.study.talk.netty.message.response.InviteGroupSelfResponse
import com.study.common.DateUtil
import com.study.common.ToastUtil
import com.study.talk.databinding.ActivityInviteFriendBinding
import com.study.talk.utils.alert.AlertUtil
import com.study.talk.utils.extras.getIntParam
import com.study.talk.utils.extras.showToast
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@AndroidEntryPoint
class InviteFriendActivity : BaseActivity() {


    companion object {
        private const val TAG = "InviteFriendJoinGroupActivity"
        const val DATA_NAME = "groupId"
    }


    private val binding: ActivityInviteFriendBinding by lazy {
        ActivityInviteFriendBinding.inflate(
            layoutInflater
        )
    }

    private val vm: InviteFriendViewModel by viewModels()
    private val adapter: UsersSelectAdapter by lazy { UsersSelectAdapter() }
    private val groupId by lazy { getIntParam(DATA_NAME) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        setSupportActionBar(binding.inviteFriendJoinGroupToolbar)
        initView(groupId)
    }


    private fun initView(groupId: Int) {
        binding.inviteFriendJoinGroupToolbar.setNavigationOnClickListener { finish() }
        binding.inviteFriendJoinGroupListView.layoutManager = LinearLayoutManager(this)
        binding.inviteFriendJoinGroupListView.adapter = adapter
        //查询还未在群里的好友
        vm.getFriendsNotInGroup(groupId)
        vm.friends.observe(this) {
            adapter.submitList(it)
        }
        binding.inviteFriendJoinGroupBt.setOnClickListener {
            val selectList = mutableListOf<Int>()
            adapter.selectMap.entries.filter { it.value }.mapTo(selectList) { it.key }
            showToast(selectList.size.toString())
            NettyClient.sendMessage(
                InviteGroupRequest(
                    groupId, selectList, DateUtil.currentTime()
                )
            )
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onInviteGroupSelfResponseMessage(response: InviteGroupSelfResponse) {
        lifecycleScope.launch {
            Snackbar.make(
                binding.inviteFriendJoinGroupBt,
                "邀请发送成功${response.successUser.size}个,失败${response.failedUser.size}个",
                Snackbar.LENGTH_SHORT
            ).setAction("查看详情") {
                AlertUtil.show(
                    this@InviteFriendActivity, "没有详情", "懒得写了"
                )
            }.show()
            delay(3000)
            finish()
        }
    }
}